﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZOVNP.Model;
using ZOVNP.Model.Abstracts;

namespace ZOVNP.WinView
{
    public partial class ProductEditControl : UserControl
    {
        private IProduct _product;
        private Workspace _workspace;

        public ProductEditControl(IProduct product, Workspace workspace)
        {
            _workspace = workspace;
            cbxProductType.DisplayMember = "Name";
            cbxProductType.ValueMember = "ID";
            cbxProductType.DataSource = _workspace.GetAllProductTypes();
            Product = product;
            InitializeComponent();
        }

        public IProduct Product
        {
            get { return _product; }
            set
            {
                _product = value;

                tbxName.Text = _product != null ? _product.Name : string.Empty;
                tbxDescription.Text = _product != null ? _product.Description : string.Empty;
                tbxProducer.Text = _product != null ? _product.Producer : string.Empty;
                tbxProperty1.Text = _product != null ? _product.Property1 : string.Empty;
                tbxProperty2.Text = _product != null ? _product.Property2 : string.Empty;
                tbxProperty3.Text = _product != null ? _product.Property3 : string.Empty;

                cbxProductType.SelectedItem = _product != null ? _product.ProductType : null;
            }
        }

        private void tbxName_TextChanged(object sender, EventArgs e)
        {
            if (_product == null)
                throw new InvalidOperationException();

            _product.Name = tbxName.Text;
        }

        private void cbxProductType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_product == null)
                throw new InvalidOperationException();

            _product.ProductType = (IProductType)cbxProductType.SelectedItem;
        }

        private void tbxDescription_TextChanged(object sender, EventArgs e)
        {
            if (_product == null)
                throw new InvalidOperationException();

            _product.Description = tbxDescription.Text;
        }

        private void tbxProducer_TextChanged(object sender, EventArgs e)
        {
            if (_product == null)
                throw new InvalidOperationException();

            _product.Producer = tbxProducer.Text;
        }

        private void tbxProperty1_TextChanged(object sender, EventArgs e)
        {
            if (_product == null)
                throw new InvalidOperationException();

            _product.Property1 = tbxProperty1.Text;
        }

        private void tbxProperty2_TextChanged(object sender, EventArgs e)
        {
            if (_product == null)
                throw new InvalidOperationException();

            _product.Property2 = tbxProperty2.Text;
        }

        private void tbxProperty3_TextChanged(object sender, EventArgs e)
        {
            if (_product == null)
                throw new InvalidOperationException();

            _product.Property3 = tbxProperty3.Text;
        }
    }
}
